package cn.kingyang.yiyi.ui.activity;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.kingyang.yiyi.R;
import cn.kingyang.yiyi.app.App;
import cn.kingyang.yiyi.ui.view.JCVideoPlayerStandard;
import cn.kingyang.yiyi.utils.AppUtil;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;


/**
 * Created by Administrator on 2017/3/2.
 */

public class VideoActivity extends AppCompatActivity {
    private String url;
    private String title;
    private TextView msg,hint;
    private ImageView pic;
    public RelativeLayout danmu;
    public List<String> danmustring;
    private boolean a=true;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        danmustring= App.getDanmu();

    }



    @Override
    protected void onResume() {
        super.onResume();
        this.title=getIntent().getStringExtra("title");
        this.url=getIntent().getStringExtra("url");
        JCVideoPlayer.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        JCVideoPlayer.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        JCVideoPlayerStandard.startFullscreen(this, JCVideoPlayerStandard.class,url,title);
        danmu= (RelativeLayout) findViewById(R.id.danmu);
        msg= (TextView) findViewById(R.id.msg);
        pic= (ImageView) findViewById(R.id.pic);
        hint= (TextView) findViewById(R.id.hint);
        if(App.level<=0) {
            hint.setText("体验区视频只能试看三十秒，升级" + App.levels.get(App.level+1) + "会员继续观看视频");
        }else{
            hint.setText("升级" + App.levels.get(App.level+1) + "会员可以观看完整视频");
        }
            anim(danmu);
            new Handler() {
                public void handleMessage(Message msg) {
                    if (msg.what == 1) {
                        finish();
                    }
                }
            }.sendEmptyMessageDelayed(1, 30000);


    }
    //    @Override
//    public void onBackPressed() {
//        if (JCVideoPlayer.backPress()) {
//            return;
//        }
//        super.onBackPressed();
//    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("1", "onPause: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("1", "onDestroy: ");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("1", "onStop: ");
        JCVideoPlayer.releaseAllVideos();
        JCVideoPlayer.clearSavedProgress(this,url);
        a=false;
        danmu.clearAnimation();
    }

    private int num=0;
    public void anim(final RelativeLayout view){
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(2000, -900);
        valueAnimator.setDuration(10000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                view.setX((Float) animation.getAnimatedValue());

            }

        });
        valueAnimator.addListener(new ValueAnimator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                msg.setText(danmustring.get(num%danmustring.size()));
                AppUtil.initImage(VideoActivity.this,pic,"http://q.qlogo.cn/headimg_dl?dst_uin="+AppUtil.getRandom(10000000,999999999)+"&spec=100&img_type=png");
                view.setY(AppUtil.getRandom(70,500));
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                num++;
                if(a)
                anim(view);
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        valueAnimator.start();

    }
}
